//
//  ViewController.m
//  电话簿
//
//  Created by apple on 17/9/4.
//  Copyright © 2017年 zhouxiong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    UITableView * tabview ;
    NSMutableArray * mutable;
    NSArray * backup;
    
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    mutable = [NSMutableArray new];
    [mutable addObject:@"滑稽，123"];
    [mutable addObject:@"阴险，1234"];
    [mutable addObject:@"傻笑，12345"];
    tabview =[[UITableView alloc]
              initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tabview.dataSource =self;
    tabview.delegate=self;
    [self.view addSubview:tabview];
    
    UISearchBar * bar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    bar.delegate = self;
    self.navigationItem.titleView = bar;
    
}

-(void) searchBar:(UISearchBar *) searchBar textDidChange:( NSString *)searchText
{
    NSLog(@"%@",searchText);
    if(searchText.length == 0){
        mutable = backup.mutableCopy;
        return ;
    }
    NSMutableArray * res =[NSMutableArray new];
    for (NSString * muta in backup ) {
        if([muta containsString:searchText]){
            [res addObject:muta];
        }
        
    }
    mutable = res;
    [tabview reloadData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return mutable.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView_
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tabview dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    NSString * str =mutable[indexPath.row];
    NSArray * arr = [str componentsSeparatedByString:@","];
    
    cell.textLabel.text= arr.firstObject;
    cell.detailTextLabel.text = arr.lastObject;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
